package com.kitbox.hl.meshow.framenwork.listener.lmpl;

import com.kitbox.hl.meshow.dao.ListenerMapper;
import com.kitbox.hl.meshow.framenwork.listener.entity.MyDelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Auther: honglei
 * @Date: 2019/8/15 10:19
 * @Description:
 */
@Primary
@Service
public class ListenerServiceRestImpl extends ListenerServiceImpl {
    private final Logger logger = LoggerFactory.getLogger(ListenerServiceRestImpl.class);
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private ListenerMapper listenerMapper;

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        super.notify(delegateExecution);
        logger.info("执行监听器URl回调：");
        String url="http://localhost:8080/pr/callbak";
        try {
            List<String> list=listenerMapper.findUrl(delegateExecution.getProcessDefinitionId(),null,0);
            if (list==null||list.size()<1)
            {
                return ;
            }
            url=list.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        MyDelegateExecution myDelegateExecution=new MyDelegateExecution();
        BeanUtils.copyProperties(delegateExecution,myDelegateExecution);
        myDelegateExecution.setEventSource(null);
        String json = gson.toJson(myDelegateExecution);
        int code=sendCallBack(url,json);
        logger.info(code+"");
    }




    @Override
    public void notify(DelegateTask delegateTask) {
        super.notify(delegateTask);
        logger.info("任务监听器URl回调：");
        String url="http://localhost:8080/pr/callbak";
        try {
            List<String> list=listenerMapper.findUrl(delegateTask.getProcessDefinitionId(),delegateTask.getProcessInstanceId(),1);
            if (list==null||list.size()<1)
            {
                return ;
            }
            url=list.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json=gson.toJson(delegateTask);
        int code=sendCallBack(url,json);
        logger.info(code+"");
    }



    private int sendCallBack(String url,String json){
        try {
            MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
            paramMap.add("json", json);
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<MultiValueMap<String, Object>> httpEntity =
                    new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
            return  responseEntity.getStatusCodeValue();
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
